<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="../static/css/prism.css" rel="stylesheet" />
</head>

<body>
    <h2>charAt方法</h2>
    <p>charAt返回指定位置的字符</p>

    <pre><code class="language-javascript">var s = new String('itbaizhan');

s.charAt(1) // "t"
s.charAt(s.length - 1) // "n"</code></pre>

    <p>如果参数为负数或者超过了字符串范围，则返回空字符串</p>
    <pre><code class="language-javascript">'itbaizhan'.charAt(-1) // ""
'itbaizhan'.charAt(9) // ""</code></pre>

    <h2>concat方法</h2>
    <p>用于连接字符串，不改变原字符串，<strong>返回一个新的字符串</strong></p>
    <pre><code class="language-javascript">var s1 = "nihao";
var s2 = " shijie !"
var new_s = s1.concat(s2);
console.log(new_s);  // nihao shijie !
console.log(s1);  // nihao</code></pre>

    <p>可以连接多个字符串</p>
    <pre><code class="language-javascript">"you can say: ".concat("hello", " world!");</code></pre>

    <p>如果参数不是字符串，则先转为字符串后再拼接</p>
    <pre><code class="language-javascript">"hello".concat(true, 123) // hellotrue123</code></pre>

    <script>
        console.log("练习开始");
        console.log("charAt方法");

        var a = new String("let's go!");
        console.log(a.charAt(0));
        console.log(a.charAt(1));
        console.log(a.charAt(2));
        console.log(a.charAt(3));

        console.log("hello".charAt(-1));
        console.log("nihao".charAt(100));

        console.log("concat方法");
        var s1 = "nihao";
        var s2 = " shijie !"
        var new_s = s1.concat(s2);
        console.log(new_s);  // nihao shijie !
        console.log(s1);  // nihao

        var s3 = "you can say: ".concat("hello", " world!");
        console.log(s3);

        var s4 = "hello".concat(true, 123); // "hellotrue123"
        console.log(s4);
        
        console.log("练习结束");
    </script>

    <script src="../static/js/prism.min.js"></script>
</body>

</html>